package com.jiyi.widget.picselect;

import java.util.List;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.utils.GlideUtil;

public class ImageGridAdapter extends BaseAdapter {

	final String TAG = getClass().getSimpleName();
	Activity act;
	List<ImageItem> dataList;
	private CheckBox currentCheck;
	private View currentShadow;

	private int max = 6;

	public ImageGridAdapter(Activity act, List<ImageItem> list, int max) {
		this.act = act;
		dataList = list;
		this.max = max;
	}

	public ImageGridAdapter(Activity act, List<ImageItem> list) {
		new ImageGridAdapter(act, list, max);
	}

	@Override
	public int getCount() {
		int count = 0;
		if (dataList != null) {
			count = dataList.size();
		}
		return count;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		final ViewHolder viewHolder;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(act).inflate(R.layout.item_image_grid, parent, false);
			viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
			viewHolder.cb_addpic = (CheckBox) convertView.findViewById(R.id.cb_addpic);
			viewHolder.rl_shadow = convertView.findViewById(R.id.rl_shadow);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final ImageItem item = dataList.get(position);
		final String imagePath = item.imagePath;
		final String thumbnailPath = item.thumbnailPath;
		if (!TextUtils.isEmpty(thumbnailPath)) {
			GlideUtil.load(act, thumbnailPath,viewHolder.image);
		} else {
			GlideUtil.load(act, imagePath,viewHolder.image);
		}
		viewHolder.cb_addpic.setClickable(false);
		if (PicSelectConstans.current.contains(imagePath)) {
			viewHolder.cb_addpic.setChecked(true);
			viewHolder.rl_shadow.setBackgroundResource(R.color.half_transparency);
		} else {
			viewHolder.cb_addpic.setChecked(false);
			viewHolder.rl_shadow.setBackgroundResource(R.color.transparency);
		}
		viewHolder.image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (PicSelectConstans.pic_tag == 2) {
					int size = PicSelectConstans.current.size();
					if (!viewHolder.cb_addpic.isChecked() && size < max) {
						viewHolder.cb_addpic.setChecked(true);
						viewHolder.rl_shadow
								.setBackgroundResource(R.color.half_transparency);
						PicSelectConstans.current.add(imagePath);

					} else if (!viewHolder.cb_addpic.isChecked() && size >= max) {
						ToastUtil.getInstances()
								.showMsg("最多只能选择" + max + "张图片");
					} else {
						viewHolder.cb_addpic.setChecked(false);
						viewHolder.rl_shadow.setBackgroundResource(R.color.transparency);
						PicSelectConstans.current.remove(imagePath);
					}
				} else if (PicSelectConstans.pic_tag == 3) {
					if (!viewHolder.cb_addpic.isChecked()) {
						viewHolder.cb_addpic.setChecked(true);
						viewHolder.rl_shadow
								.setBackgroundResource(R.color.half_transparency);
						PicSelectConstans.current.add(imagePath);

					} else {
						viewHolder.cb_addpic.setChecked(false);
						viewHolder.rl_shadow.setBackgroundResource(R.color.transparency);
						PicSelectConstans.current.remove(imagePath);
					}
				} else if (PicSelectConstans.pic_tag == 4) {
					// 多张模式2 但是不能超过4张
					int size = PicSelectConstans.current.size();
					if (!viewHolder.cb_addpic.isChecked() && size < 3) {
						viewHolder.cb_addpic.setChecked(true);
						viewHolder.rl_shadow
								.setBackgroundResource(R.color.half_transparency);
						PicSelectConstans.current.add(imagePath);

					} else if (!viewHolder.cb_addpic.isChecked() && size >= 3) {
						ToastUtil.getInstances().showMsg("最多只能选择3张图片");
					} else {
						viewHolder.cb_addpic.setChecked(false);
						viewHolder.rl_shadow.setBackgroundResource(R.color.transparency);
						PicSelectConstans.current.remove(imagePath);
					}
				}

				else {

					if (currentCheck != null && !currentCheck.equals(viewHolder.cb_addpic)) {
						currentCheck.setChecked(false);
						currentShadow
								.setBackgroundResource(R.color.transparency);
					}

					currentCheck = viewHolder.cb_addpic;
					currentShadow = viewHolder.rl_shadow;

					if (viewHolder.cb_addpic.isChecked()) {
						viewHolder.cb_addpic.setChecked(false);
						viewHolder.rl_shadow.setBackgroundResource(R.color.transparency);
						PicSelectConstans.headUrl = "";
					} else {
						viewHolder.cb_addpic.setChecked(true);
						viewHolder.rl_shadow
								.setBackgroundResource(R.color.half_transparency);
						PicSelectConstans.headUrl = imagePath;
					}

				}
			}
		});
		return convertView;
	}
	
	class ViewHolder {		
		private ImageView image;
		private CheckBox cb_addpic;
		private View rl_shadow;
	}
}
